package com.zhongyu.item.consumer.service.Impl;

import com.zhongyu.comm.pojo.Image;
import com.zhongyu.comm.pojo.Item;
import com.zhongyu.comm.pojo.ItemType;
import com.zhongyu.item.consumer.client.ImageClient;
import com.zhongyu.item.consumer.client.ItemClient;
import com.zhongyu.item.consumer.client.ItemTypeClient;
import com.zhongyu.item.consumer.service.IndexFenLeiService;
import com.zhongyu.item.consumer.vo.FenLeiVo;
import com.zhongyu.item.consumer.vo.HostItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service("IndexFenLeiService")
public class IndexFenLeiServiceImpl implements IndexFenLeiService {
    @Resource
    private ItemClient itemClient;
    @Resource
    private ItemTypeClient itemTypeClient;
    @Resource
    private ImageClient imageClient;

    @Override
    public List<FenLeiVo> searchFenLeis() {
        List<ItemType> itemTypes = itemTypeClient.findByLevel1();
        return itemTypes.stream().map(itemType -> {
            FenLeiVo fenLeiVo = new FenLeiVo();
            fenLeiVo.setId(itemType.getId());
            fenLeiVo.setItemType(itemType.getItemType());
            fenLeiVo.setLevel(itemType.getLevel());
            fenLeiVo.setParent(itemType.getParent());
            fenLeiVo.setAliasName(itemType.getAliasName());
            List<ItemType> list = itemTypeClient.findByLevel2();
            for (ItemType itemType1 : list) {
                FenLeiVo fenLeiVo1 = new FenLeiVo();
                fenLeiVo1.setId(itemType.getId());
                fenLeiVo1.setItemType(itemType.getItemType());
                fenLeiVo1.setLevel(itemType.getLevel());
                fenLeiVo1.setParent(itemType.getParent());
                fenLeiVo1.setAliasName(itemType.getAliasName());
//                BeanUtils.copyProperties(itemType1,fenLeiVo1);
                fenLeiVo.getChildren().add(fenLeiVo1);
            }
            ;
            List<Item> list1 = itemClient.findrejiemu(itemType.getId());
            for (Item item : list1) {
                HostItemVo hostItemVo = new HostItemVo();
                BeanUtils.copyProperties(item, hostItemVo);
                Image image = imageClient.findOne(item.getId(), 2, 1);
                hostItemVo.setImgUrl(image.getImgUrl());
                fenLeiVo.getHotItems().add(hostItemVo);
            }
            return fenLeiVo;
        }).collect(Collectors.toList());
    }
}
